package com.atguigu.yygh.hosp.controller;

import com.atguigu.yygh.common.R;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author zhmwi
 * @Date 2021/6/29
 * @Dersciption
 */

@RestController
@Api("医院设置接口")
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {
    @Autowired
    private HospitalSetService hospitalSetService;
    //模拟登陆信息开始-----------
    @PostMapping("/login")
    public R login(){
        return R.ok().data("token","admin");
    }

    @GetMapping("/info")
    public R info(){
        return R.ok()
                .data("roles","[admin]")
                .data("name","admin")
                .data("avatar","");
    }

    @PostMapping("logout")
    public R logout(){
        return R.ok();
    }
    //模拟登陆信息结束---------


    //1.查询所有医院设置
    @ApiOperation("查询所有医院设置")
    @GetMapping("getList")
    public R getHospitalSetList(){
        List<HospitalSet> list = hospitalSetService.list();
        return R.ok().data("hospitalSetList",list);
    }
    //2.删除医院设置
    @ApiOperation("删除医院设置")
    @DeleteMapping("delById/{id}")
    public R delHospitalSetById(@PathVariable Long id){
        return R.ok().success(hospitalSetService.removeById(id));
    }
    //3.分页查询医院设置
    @ApiOperation("分页查询医院设置")
    @GetMapping("/getByPage/{page}/{limit}")
    public R getHospitalSetByPage(@ApiParam("当前页") @PathVariable Long page,
                                  @ApiParam("每页显示数") @PathVariable Long limit){
        Page<HospitalSet> pageParam = new Page<>(page,limit);
        hospitalSetService.page(pageParam,null);
        List<HospitalSet> hospitalSetList = pageParam.getRecords();
        long total = pageParam.getTotal();
        return R.ok().data("rows",hospitalSetList).data("total",total);
    }
    //4.带条件带分页查询医院设置
    @ApiOperation("带条件带分页查询医院设置")
    @PostMapping("/getByPageAndCondition/{page}/{limit}")
    public R getHospitalSetByPageByCondition(@ApiParam("当前页") @PathVariable Long page,
                                             @ApiParam("每页显示数") @PathVariable Long limit,
                                             @ApiParam("查询条件") @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo){
        Page<HospitalSet> pageParam = new Page<>(page, limit);
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();
        //查询条件不为空
        if (hospitalSetQueryVo != null){
            //医院名称
            String hosname = hospitalSetQueryVo.getHosname();
            //医院代号
            String hoscode = hospitalSetQueryVo.getHoscode();

            if (!StringUtils.isEmpty(hosname)){
                queryWrapper.like("hosname",hosname);
            }
            if (!StringUtils.isEmpty(hoscode)){
                queryWrapper.eq("hoscode",hoscode);
            }
        }
        hospitalSetService.page(pageParam,queryWrapper);
        List<HospitalSet> rows = pageParam.getRecords();
        long total = pageParam.getTotal();
        return R.ok().data("rows",rows).data("total",total);
    }
    //5.新增医院设置
    @ApiOperation("新增医院设置")
    @PostMapping("/save")
    public R saveHospitalSet(@ApiParam("医院设置")@RequestBody HospitalSet hospitalSet){
        boolean save = hospitalSetService.save(hospitalSet);
        if (save){
            return R.ok().message("新增医院设置成功");
        }else{
            return R.error().message("新增医院设置失败");
        }
    }
    //6.根据Id查询医院设置
    @ApiOperation("根据id查询医院设置")
    @GetMapping("getById/{id}")
    public R getById(@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return R.ok().data("hospitalSet",hospitalSet);
    }
    //7.根据id修改医院设置
    @ApiOperation("根据id修改医院设置")
    @PostMapping("updateById")
    public R updateById(@RequestBody HospitalSet hospitalSet){
        boolean b = hospitalSetService.updateById(hospitalSet);
        if (b){
            return R.ok().message("修改医院设置成功");
        }else{
            return R.error().message("修改医院设置失败");
        }
    }
    //8.批量删除
    @ApiOperation("批量删除")
    @DeleteMapping("deleteByIds")
    public R deleteByIds(@RequestBody List<Long> ids){
        boolean b = hospitalSetService.removeByIds(ids);
        if (b){
            return R.ok().message("批量删除医院设置成功");
        }else{
            return R.error().message("批量删除医院设置失败");
        }
    }
    //9.医院设置的锁定和解锁
    @ApiOperation("医院设置的锁定和解锁")
    @PutMapping("lockOrUnlockHospitalSet/{id}/{status}")
    public R lockOrUnlock(@ApiParam("医院设置id")@PathVariable Long id,
                          @ApiParam("要设置的医院设置状态") @PathVariable Integer status){
        //根据ID查询医院设置信息
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        hospitalSet.setStatus(status);
        //修改医院设置状态
        hospitalSetService.updateById(hospitalSet);
        return R.ok();
    }
}
